﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lottery.Base;

namespace Lottery.Real
{
    public sealed class MyOwnNumbersSelector : INumbersSelector
    {
        public IEnumerable<int> SelectNumbers(int numbersCount, IGameHistory gameHistory = null)
        {
            var orderedHistory = gameHistory.History.OrderByDescending(gh => gh.Date).Take(10);
            var hitCount = new Dictionary<int, int>();

            foreach (var history in orderedHistory)
            {
                foreach (var number in history.NumberSet.Numbers)
                {
                    if (!hitCount.ContainsKey(number))
                    {
                        hitCount.Add(number, 1);
                    }
                    else
                    {
                        hitCount[number] = hitCount[number] + 1;
                    }
                }
            }

            return hitCount.OrderBy(hc => hc.Value).Select(hc => hc.Key).Take(numbersCount).ToList();
        }
    }
}
